﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HAUtil;

namespace HA.ModFlow.Domain.FileLayer
{
    public class PDiscretization:Package
    {
        public PDiscretization()
        {
            this._fileType = FileType.DIS;
            this.Nunit = 14;

            this.Fname = "solution.dis";


        }


        uint _NLAY;

        public uint NLAY
        {
            get { return _NLAY; }
            set { _NLAY = value; }
        }

        uint _NROW;

        public uint NROW
        {
            get { return _NROW; }
            set { _NROW = value; }
        }

        uint _NCOL;

        public uint NCOL
        {
            get { return _NCOL; }
            set { _NCOL = value; }
        }

        uint _NPER;

        public uint NPER
        {
            get { return _NPER; }
            set { _NPER = value; }
        }

        ITMUNI _ITMUNI = ITMUNI.days;

        public ITMUNI ITMUNI
        {
            get { return _ITMUNI; }
            set { _ITMUNI = value; }
        }

        ILUNI _ILUNI = ILUNI.meters;

        public ILUNI ILUNI
        {
            get { return _ILUNI; }
            set { _ILUNI = value; }
        }

        int[] _LAYCBD;//NLAY

        /// <summary>
        /// 数量等于NLAY，指示该层下方是否含有隔水层，
        /// 0 表示没有，不等于0表示有，最后一层必须写为0
        /// </summary>
        public int[] LAYCBD
        {
            get { return _LAYCBD; }
            set { _LAYCBD = value; }
        }


        double[] _DELR;

        public double[] DELR
        {
            get { return _DELR; }
            set { _DELR = value; }
        }


        double[] _DELC;

        public double[] DELC
        {
            get { return _DELC; }
            set { _DELC = value; }
        }



        double[,] _Top;

        public double[,] Top
        {
            get { return _Top; }
            set { _Top = value; }
        }

       


        List<double[,]> _BOTM=new List<double[,]> ();

        public List<double[,]> BOTM
        {
            get { return _BOTM; }
          
        }


       

        List<DisStep> _Steps=new List<DisStep> ();

        public List<DisStep> Steps
        {
            get { return _Steps; }
            
        }

       

        /// <summary>
        /// 向外输出文件
        /// </summary>
        /// <param name="path"></param>

        public override void WriteFile(string path)
        {
            string content = "";
            //添加#Text
            content += "# HuanAnKeJi DIS File\r\n";

            //添加层，行、列数，压力相关信息，时间单位，长度单位等信息
            content += "  " + string.Format("{0,10}",_NLAY) + "  " + string.Format("{0,10}",_NROW) + "  " + string.Format("{0,10}",_NCOL)  +"  " +string.Format("{0,10}",_NPER) + "  " +string.Format("{0,10}",(int)_ITMUNI) + "  "+string.Format("{0,10}",(int)_ILUNI) + "  NLAY,NROW,NCOL,NPER,ITMUNI,LENUNI\r\n";

            //添加LAYCBD(NLAY)信息
            for (int i = 0; i < _LAYCBD.Length; i++)
            {
                content += "  " + string.Format("{0,10}",_LAYCBD[i]);
            }
            content += "  LAYCBD\r\n";

            //添加DELR(NCOL) - U1DREL
            if (IsConstant(_DELR))
            {
                content += "CONSTANT  " + _DELR[0].ToString("0.00000E+00") + "  DELR\r\n";
            }
            else
            {
                if (GetArrayString(_DELR, ref helpString))
                {
                    content += "INTERNAL   1.0 (FREE)  1  DELR\r\n";
                    content += helpString;
                }
            }
           
            //添加DELC(NROW) - U1DREL
            if (IsConstant(_DELC))
            {
                content += "CONSTANT  " + _DELC[0].ToString("0.00000E+00") + "  DELC\r\n";
            }
            else
            {
                if (GetArrayString(_DELC, ref helpString))
                {
                    content += "INTERNAL   1.0 (FREE)  1  DELC\r\n";
                    content += helpString;
                }
            }

            //添加TOP层信息
            if(IsConstant (_Top))
            {
                content += "CONSTANT  " + _Top[0, 0].ToString("0.00000E+00") + "  TOP\r\n";
            }
            else
            {
                if (GetArrayString(_Top, ref helpString))
                {
                    content += "INTERNAL   1.0 (FREE)  19  TOP\r\n";
                    content += helpString;
                }
            }

            //添加BOT层信息
            counter = 1;
            foreach (double[,] bot in _BOTM)
            {
                if (IsConstant(bot))
                {
                    content += "CONSTANT  " + bot[0, 0].ToString("0.00000E+00") + "  BOT layer  " + counter + "\r\n";
                }
                else
                {
                    if (GetArrayString(bot, ref helpString))
                    {
                        content += "INTERNAL   1.0 (FREE)  19  BOT layer  " + counter + "\r\n";
                        content += helpString;
                    }
                }
                counter++;
            }

            //添加PERLEN NSTP TSMULT Ss/Tr 信息
            helpString = "";
            foreach (DisStep disStep in _Steps)
            {
                helpString += disStep.GetString();
            }

            content += helpString;

            string fname = path + this.Fname;

            FileOperations.WriteFile(fname, content);
        }



    }
}
